import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login.vue'
import home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [{ path: '/', redirect: '/login' }, {
    path: '/login',
    component: login
}, {
    path: '/home',
    component: home
}]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //如果用户访问的是登录页，直接放行
    if (to.path === '/login') return next();
    //如果方式的是需要权限的页面
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) return next('/login')
    next();
})

export default router